package com.wuyong;

public class Array {

    private int size;
    private int[] data;

    public Array(int capacity) {
        data = new int[capacity];
        size = 0;
    }

    public Array() {
        this(10);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int get(int index) {
        if (index >  size || index < 0) {
            throw new IllegalArgumentException("insert fail, index illegal");
        }
        return data[index];
    }

    public void set(int index, int e) {
        if (index >  data.length || index < 0) {
            throw new IllegalArgumentException("insert fail, index illegal");
        }
         data[index] = e;
    }

    public void addLast(int e) {
        if (size ==  data.length) {
            throw new IllegalArgumentException("add e fail, th data is full");
        }
        data[size] = e;
        size++;
    }

    public void addFirst(int e) {
        insert(e, 0);
    }

    public void insert(int e, int index) {
        if (size ==  data.length) {
            throw new IllegalArgumentException("add e fail, the data is full");
        }
        if (index >  size || index < 0) {
            throw new IllegalArgumentException("insert fail, index acquire not satisfacty");
        }
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = e;
        size++;
    }

    public boolean contain(int e) {
        for (int x : data) {
            if (x == e) {
                return true;
            }
        }
        return false;
    }

    public void remove() {

    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(String.format("Array size is %d, capacity is %d .", size, data.length));
        result.append("[");
        for (int i = 0; i < size; i++) {
            result.append(data[i]);
            if (i != size - 1) {
                result.append(",");
            }
        }
        result.append("]");
        return result.toString();
    }

    private void resize(int newLen) {
        int[] newData = new int[newLen];
        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }
}
